/**
 * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
 *
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.admin.controller;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import io.jpress.core.JBaseCRUDController;
import io.jpress.core.interceptor.ActionCacheClearInterceptor;
import io.jpress.model.*;
import io.jpress.model.query.*;
import io.jpress.router.RouterMapping;
import io.jpress.router.RouterNotAllowConvert;

import java.math.BigInteger;
import java.sql.SQLException;

@RouterMapping(url = "/admin/data", viewPath = "/WEB-INF/admin/data")
@Before(ActionCacheClearInterceptor.class)
@RouterNotAllowConvert
public class _DataController extends JBaseCRUDController<HouseData> {

	public void index() {
		Page<HouseData> page = HouseDataQuery.me().paginate(getPageNumber(), getPageSize(), null);

		setAttr("hallMap",HallEnum.getHallMap());
		setAttr("page", page);

		setAttr("include", "_index_include.html");
	}

	public void edit() {
		BigInteger id = getParaToBigInteger("id");
		if (id != null) {
			setAttr("houseData", HouseDataQuery.me().findById(id));
		}

		setAttr("hallMap",HallEnum.getHallMap());

		setAttr("include", "_edit_include.html");

	}

	public void save() {

		final HouseData houseData = getModel(HouseData.class);

		if (houseData.getTime() == null) {
			renderAjaxResultForError("时间点不能为空。");
			return;
		}

		boolean saved = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {

				if (!houseData.saveOrUpdate()) {
					return false;
				}

				return true;
			}
		});

		if (saved) {
			renderAjaxResultForSuccess();
		} else {
			renderAjaxResultForError();
		}

		houseData.saveOrUpdate();

		renderAjaxResultForSuccess("ok");
	}

	@Override
	public void delete() {
		BigInteger id = getParaToBigInteger("id");
		if (id == null) {
			renderAjaxResultForError();
			return;
		}

		super.delete();
	}

	public void listNotData() {

		Page<NotData> page = NotDataQuery.me().paginate(getPageNumber(), getPageSize(), null);
		setAttr("hallMap",HallEnum.getHallMap());
		setAttr("page", page);

		setAttr("include", "_data_include.html");
	}

	public void editNotData() {
		BigInteger id = getParaToBigInteger("id");
		if (id != null) {
			setAttr("notData", NotDataQuery.me().findById(id));
		}
		setAttr("hallMap",HallEnum.getHallMap());

		setAttr("include", "_edit_not_data_include.html");

	}

	public void saveNotData() {

		final NotData notData = getModel(NotData.class);

		if (notData.getNeedNum()==null) {
			renderAjaxResultForError("需求量不能为空。");
			return;
		}
		if (notData.getOrderNum()==null) {
			renderAjaxResultForError("订单数不能为空。");
			return;
		}
		if (notData.getTradeNum()==null) {
			renderAjaxResultForError("交易额不能为空。");
			return;
		}

		boolean saved = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {

				if (!notData.saveOrUpdate()) {
					return false;
				}

				return true;
			}
		});

		if (saved) {
			renderAjaxResultForSuccess();
		} else {
			renderAjaxResultForError();
		}
	}

	public void deleteNotData() {
		BigInteger id = getParaToBigInteger("id");
		NotDataQuery.me().delete(id);
	}

	public void editSummary() {
		BigInteger id = getParaToBigInteger("id");
		if (id != null) {
			setAttr("summary", new Summary().findById(id));
		}

		setAttr("hallMap",HallEnum.getHallMap());

		setAttr("include", "_edit_summary_include.html");

	}

	public void saveSummary() {

		final Summary summary = getModel(Summary.class);

		boolean saved = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {

				if (!summary.saveOrUpdate()) {
					return false;
				}

				return true;
			}
		});

		if (saved) {
			renderAjaxResultForSuccess();
		} else {
			renderAjaxResultForError();
		}
	}

	public void listMsg() {

		Page<Msg> page = MsgQuery.me().paginate(getPageNumber(), getPageSize());
		setAttr("typeMap",MsgTypeEnum.getTypeMap());
		setAttr("page", page);

		setAttr("include", "_msg_include.html");
	}

	public void editMsg() {
		BigInteger id = getParaToBigInteger("id");
		if (id != null) {
			setAttr("msg", MsgQuery.me().findById(id));
		}
		setAttr("typeMap",MsgTypeEnum.getTypeMap());

		setAttr("include", "_edit_msg_include.html");

	}

	public void saveMsg() {

		final Msg msg = getModel(Msg.class);

		if (msg.getTitle()==null) {
			renderAjaxResultForError("标题不能为空。");
			return;
		}
		if (msg.getUrl()==null) {
			renderAjaxResultForError("url不能为空。");
			return;
		}

		boolean saved = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {

				if (!msg.saveOrUpdate()) {
					return false;
				}

				return true;
			}
		});

		if (saved) {
			renderAjaxResultForSuccess();
		} else {
			renderAjaxResultForError();
		}
	}

	public void deleteMsg() {
		BigInteger id = getParaToBigInteger("id");
		MsgQuery.me().delete(id);
	}

	/***********************天津项目开始**********************/
	public void listSsj() {

		Page<TjSsj> page = TjSsjQuery.me().paginate(getPageNumber(), getPageSize(),"day");
		setAttr("page", page);

		setAttr("include", "_ssj_include.html");
	}

	public void editSsj() {
		BigInteger id = getParaToBigInteger("id");
		if (id != null) {
			setAttr("tjSsj", TjSsjQuery.me().findById(id));
		}

		setAttr("include", "_edit_ssj_include.html");

	}

	public void saveSsj() {

		final TjSsj tjSsj = getModel(TjSsj.class);

		if (tjSsj.getDay()==null) {
			renderAjaxResultForError("日期不能为空。");
			return;
		}

		boolean saved = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {

				if (!tjSsj.saveOrUpdate()) {
					return false;
				}

				return true;
			}
		});

		if (saved) {
			renderAjaxResultForSuccess();
		} else {
			renderAjaxResultForError();
		}
	}

	public void deleteSsj() {
		BigInteger id = getParaToBigInteger("id");
		TjSsjQuery.me().deleteById(id);
	}

	/********************科技成果情况**********************/
	public void listKjcg() {

		Page<TjKjcg> page = TjKjcgQuery.me().paginate(getPageNumber(), getPageSize(),"year");
		setAttr("page", page);

		setAttr("include", "_kjcg_include.html");
	}

	public void editKjcg() {
		BigInteger id = getParaToBigInteger("id");
		if (id != null) {
			setAttr("tjKjcg", TjKjcgQuery.me().findById(id));
		}

		setAttr("include", "_edit_kjcg_include.html");

	}

	public void saveKjcg() {

		final TjKjcg tjKjcg = getModel(TjKjcg.class);

		if (tjKjcg.getYear()==null) {
			renderAjaxResultForError("年份不能为空。");
			return;
		}

		boolean saved = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {

				if (!tjKjcg.saveOrUpdate()) {
					return false;
				}

				return true;
			}
		});

		if (saved) {
			renderAjaxResultForSuccess();
		} else {
			renderAjaxResultForError();
		}
	}

	public void deleteKjcg() {
		BigInteger id = getParaToBigInteger("id");
		TjKjcgQuery.me().deleteById(id);
	}

	/********************大众创业情况**********************/
	public void listDzcy() {

		Page<TjDzcy> page = TjDzcyQuery.me().paginate(getPageNumber(), getPageSize(),"month");
		setAttr("page", page);

		setAttr("include", "_dzcy_include.html");
	}

	public void editDzcy() {
		BigInteger id = getParaToBigInteger("id");
		if (id != null) {
			setAttr("tjDzcy", TjDzcyQuery.me().findById(id));
		}

		setAttr("include", "_edit_dzcy_include.html");

	}

	public void saveDzcy() {

		final TjDzcy tjDzcy = getModel(TjDzcy.class);

		if (tjDzcy.getMonth()==null) {
			renderAjaxResultForError("月份不能为空。");
			return;
		}

		boolean saved = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {

				if (!tjDzcy.saveOrUpdate()) {
					return false;
				}

				return true;
			}
		});

		if (saved) {
			renderAjaxResultForSuccess();
		} else {
			renderAjaxResultForError();
		}
	}

	public void deleteDzcy() {
		BigInteger id = getParaToBigInteger("id");
		TjDzcyQuery.me().deleteById(id);
	}

	/********************天津其他数据（区县技术交易额、专利数据、上市企业、滨海新区双创企业）*********************/
	public void editTjSummary() {
		BigInteger id = new BigInteger("1");
		if (id != null) {
			setAttr("summary", new TjSummary().findById(id));
		}

		setAttr("include", "_edit_tjsummary_include.html");

	}

	public void saveTjSummary() {

		final TjSummary tjSummary = getModel(TjSummary.class);

		boolean saved = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {

				if (!tjSummary.saveOrUpdate()) {
					return false;
				}

				return true;
			}
		});

		if (saved) {
			renderAjaxResultForSuccess();
		} else {
			renderAjaxResultForError();
		}
	}

	/********************研究项目成果**********************/
	public void listYjxmcg() {

		Page<TjYjxmcg> page = TjYjxmcgQuery.me().paginate(getPageNumber(), getPageSize());
		setAttr("page", page);

		setAttr("include", "_yjxmcg_include.html");
	}

	public void editYjxmcg() {
		BigInteger id = getParaToBigInteger("id");
		if (id != null) {
			setAttr("tjYjxmcg", TjYjxmcgQuery.me().findById(id));
		}

		setAttr("include", "_edit_yjxmcg_include.html");

	}

	public void saveYjxmcg() {

		final TjYjxmcg tjYjxmcg = getModel(TjYjxmcg.class);

		if (tjYjxmcg.getName()==null) {
			renderAjaxResultForError("名称不能为空。");
			return;
		}

		boolean saved = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {

				if (!tjYjxmcg.saveOrUpdate()) {
					return false;
				}

				return true;
			}
		});

		if (saved) {
			renderAjaxResultForSuccess();
		} else {
			renderAjaxResultForError();
		}
	}

	public void deleteYjxmcg() {
		BigInteger id = getParaToBigInteger("id");
		TjYjxmcgQuery.me().deleteById(id);
	}

	public void jkgf() {
		setAttr("include", "_jkgf_include.html");
	}
}
